package com.zh.wl;


import org.apache.axis.utils.StringUtils;
import org.junit.Test;

import javax.xml.rpc.ServiceException;
import java.rmi.RemoteException;

/**
 * 在线订单模块测试
 * 本次测试未使用cfx框架对webservice进行交互,后续开发应使用框架进行开发,提高效率!spring+idea+maven+cfx协作开发服务端
 * 利用wbservice技术获取客户提供的wsdl接口,书写客户端代码进行调用webservice写好的逻辑进行交互
 */
public class Wlendent {
    /**
     * 根据传入的 ReferenceNumber 引用单号，可以是客户单号或服务商单号查询订单信息
     *
     * @throws RemoteException
     * @throws ServiceException
     */
    @Test
    public void findOrder() throws RemoteException, ServiceException {
        OrderOnlineServiceExtImplServiceLocator serviceLocator = new OrderOnlineServiceExtImplServiceLocator();
        IOrderOnlineService service = serviceLocator.getOrderOnlineServiceExtImplPort();
        FindOrderRequest findOrderRequest = new FindOrderRequest();
        String str[] = {"100212313322223123"};
        String Token = "A96A6ECEA7E103BA0EA877B69D28A833";
        findOrderRequest.setOrderNo(str);
        FindOrderResponse findOrderResponse = service.findOrderService(Token, findOrderRequest);
        if (findOrderResponse.getAck().equals("Success")) {
            OrderItem[] orderItems = findOrderResponse.getOrderItem();
            System.out.println("返回客户订单单号     " + orderItems[0].getOrderNo());
            System.out.println("返回追踪单号     " + orderItems[0].getTrackingNumber());
        }
        if (findOrderResponse.getAck().equals("Failure")) {
            Error[] errors = findOrderResponse.getErrors();
            System.out.println(errors[0].getCnMessage());
        }
    }

    /**
     * 创建订单需要将订单全部信息上传交给webservice服务端进行逻辑上的处理
     *
     * @throws ServiceException
     * @throws RemoteException
     */
    @Test
    public void creatOrder() {
        OrderOnlineServiceExtImplServiceLocator serviceLocator = new OrderOnlineServiceExtImplServiceLocator();
        IOrderOnlineService service = null;
        try {
            service = serviceLocator.getOrderOnlineServiceExtImplPort();
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        CreateOrderRequest[] createOrderRequests = new CreateOrderRequest[2];
        createOrderRequests[0] = new CreateOrderRequest();
        createOrderRequests[0].setOrderNo("10221556461254");
        // 服务商跟踪号码，由系统提供；客户如果有相应服务商跟踪号，也可以提供，但我司会校验是否有效，无效则重新分配
        createOrderRequests[0].setTrackingNumber("122232231256");
        createOrderRequests[0].setPaymentCode("1003");
        createOrderRequests[0].setCargoCode("P");
        createOrderRequests[0].setPaymentCode("P");
        createOrderRequests[0].setInitialCountryCode("CN");
        createOrderRequests[0].setDestinationCountryCode("RU");
        createOrderRequests[0].setPieces("2");
        createOrderRequests[0].setInsurType("1P");
        createOrderRequests[0].setInsurValue("8");
        createOrderRequests[0].setBuyerId("2333");
        createOrderRequests[0].setReturnSign("N");
        createOrderRequests[0].setCustomerWeight("5");
        createOrderRequests[0].setTransactionId("123456");
        createOrderRequests[0].setShipperCompanyName("百盛");
        createOrderRequests[0].setShipperName("张三");
        createOrderRequests[0].setShipperStateOrProvince("广东");
        createOrderRequests[0].setShipperCity("广州");
        createOrderRequests[0].setShipperAddress("科技路");
        //以下为收货人信息,切必须为俄罗斯地区
        createOrderRequests[0].setConsigneeName("Вячеслав Никифоров");
        createOrderRequests[0].setStreet("Выборнова 3 кв.25");
        createOrderRequests[0].setCity("Архангельск");
        createOrderRequests[0].setStateOrProvince("АРХАНГЕЛЬСКАЯ ОБЛАСТ ");
        createOrderRequests[0].setProductCode("AP");//产品代码，指小包挂号、小包平邮等，参照产品代码表，(运输方式)
        createOrderRequests[0].setConsigneeTelephone("186296536250");//收件人电话
        createOrderRequests[0].setConsigneePostCode("163000");//收件人邮编
        createOrderRequests[0].setMctCode("3");//货物类型代码
        DeclareInvoice[] declareInvoice = new DeclareInvoice[1];
        //海关信息
        declareInvoice[0] = new DeclareInvoice();
        declareInvoice[0].setEName("enpornt");//申报海关英文名
        declareInvoice[0].setDeclareUnitCode("SET");//申报货物单位
        declareInvoice[0].setUnitPrice("50");//申报单价
        createOrderRequests[0].setDeclareInvoice(declareInvoice);
        String Token = "A96A6ECEA7E103BA0EA877B69D28A833";
        CreateOrderResponse[] createOrderResponses = new CreateOrderResponse[0];
        try {
            createOrderResponses = service.createOrderService(Token, createOrderRequests);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        System.out.println("返回的追踪单号    " + createOrderResponses[0].getTrackingNumber());
        System.out.println("操作状态   " + createOrderResponses[0].getAck());
        if (createOrderResponses[0].getAck().equals("Failure")) {
            System.out.println("如果有错误返回错误信息对象     " + createOrderResponses[0].getErrors());
            Error[] errors = new Error[1];
            errors = createOrderResponses[0].getErrors();
            System.out.println("错误信息Code     " + errors[0].getCode() + "      \n" + "错误信息详细信息     " + errors[0].getCnMessage());
        }
    }

    /**
     * 修改订单只需要将有修改的信息上传,服务端自行处理ssss
     */
    @Test
    public void modifOrder() {
        OrderOnlineServiceExtImplServiceLocator orderOnlineServiceExtImplServiceLocator = new OrderOnlineServiceExtImplServiceLocator();
        IOrderOnlineService service = null;
        try {
            service = orderOnlineServiceExtImplServiceLocator.getOrderOnlineServiceExtImplPort();
            ModifyOrderRequest[] modifyOrderRequest = null;
            try {
                ModifyOrderResponse[] modifyOrderResponses = service.modifyOrderService(null, modifyOrderRequest);
                for (int i = 1; i < modifyOrderResponses.length; i++) {
                    System.out.println(modifyOrderRequest[i]);
                }
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        } catch (ServiceException e) {
            e.printStackTrace();
        }

    }

    /**
     * 删除订单需要订单编号多个或者一个
     */
    @Test
    public void removeOrder() {
        OrderOnlineServiceExtImplServiceLocator serviceLocator = new OrderOnlineServiceExtImplServiceLocator();
        try {
            IOrderOnlineService service = serviceLocator.getOrderOnlineServiceExtImplPort();
            String[] strings = {"100212313322223123"};
            String Token = "A96A6ECEA7E103BA0EA877B69D28A833";
            RemoveOrderResponse[] removeOrderResponses = service.removeOrderService(Token, strings);
            System.out.println("返回操作状态    " + removeOrderResponses[0].getAck());
            if (removeOrderResponses[0].getAck().equals("Failure")) {
                Error[] error = removeOrderResponses[0].getErrors();
                System.out.println("错误信息   " + error[0].getCnMessage());
            }
        } catch (ServiceException e) {
            e.printStackTrace();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    /**
     * 预订单目前还没搞懂0.0
     */
    @Test
    public void preAlertOrder() {
        OrderOnlineServiceExtImplServiceLocator serviceLocator = new OrderOnlineServiceExtImplServiceLocator();
        try {
            IOrderOnlineService service = serviceLocator.getOrderOnlineServiceExtImplPort();
            String[] strings = {"102"};
            String arg0 = "A96A6ECEA7E103BA0EA877B69D28A833";
            PreAlertOrderResponse[] preAlertOrderResponses = service.preAlertOrderService(arg0, strings);
            System.out.println(preAlertOrderResponses.length);
            System.out.println(preAlertOrderResponses[0].getTimestamp() + "\n" + preAlertOrderResponses[0].getAck());
        } catch (ServiceException e) {
            e.printStackTrace();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
}
